<template>

  <div class="app-container">

    <h2 style="text-align: center;">发布新课程</h2>

    <el-steps :active="1" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="提交审核"/>
    </el-steps>

    <el-form label-width="120px">

      <el-form-item label="课程标题">
        <el-input v-model="courseInfo.title" placeholder=" 示例：机器学习项目课：从基础到搭建项目视频课程。专业名称注意大小写"/>
      </el-form-item>

      <!-- 所属分类 TODO -->
      <el-form-item label="请选择课程分类">
        <el-select
          v-model="courseInfo.subjectParentId"
          placeholder="请选择一级分类"
          @change="changeSubjectTwoList">
          <el-option
            v-for="subjectOne in subjectOneList"
            :key="subjectOne.id"
            :label="subjectOne.title"
            :value="subjectOne.id"
            />
        </el-select>
        <el-select
          v-model="courseInfo.subjectId"
          placeholder="请选择二级分类">
          <el-option
            v-for="subjectTwo in subjectTwoList"
            :key="subjectTwo.id"
            :label="subjectTwo.title"
            :value="subjectTwo.id"/>
        </el-select>
      </el-form-item>

      <!-- 课程讲师 TODO -->
      <el-form-item label="课程讲师">
        <el-select
          v-model="courseInfo.teacherId"
          placeholder="请选择">
          <el-option
            v-for="teacher in teacherList"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"/>
        </el-select>
      </el-form-item>
       <el-form-item label="总课时">
           <el-input-number :min="0" v-model="courseInfo.lessonNum" controls-position="right" placeholder="请填写课程的总课时数"/>
       </el-form-item>

        <!-- 课程简介 TODO -->
        <!-- 课程简介-->
      <el-form-item label="课程简介">
          <tinymce :height="300" v-model="courseInfo.description"/>
      </el-form-item>
        <!-- 课程封面 TODO -->
      <el-form-item label="课程封面">

        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_API+'/eduoss/fileoss/upload/avatar'"
          class="avatar-uploader">
          <img :src="courseInfo.cover">
        </el-upload>

      </el-form-item>

      <el-form-item label="课程价格">
        <el-input-number :min="0" v-model="courseInfo.price" controls-position="right" placeholder="免费课程请设置为0元"/> 元
      </el-form-item>

      <el-form-item label-width="120px">
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next">保存并下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import course from '@/api/edu/course'
import subject from '@/api/edu/subject'
import Tinymce from '@/components/Tinymce'
export default {
  components: { Tinymce },
  data() {
    return {
      saveBtnDisabled: false, // 保存按钮是否禁用
      // 页面表单信息
      courseInfo:{
        title: '',
        // subjectId: '',
        teacherId: '',
        lessonNum: 0,
        description: '',
        cover: 'http://littlefinger.oss-cn-beijing.aliyuncs.com/2022/01/07/9ed3b33a-005e-44d7-819d-8984501b88bcQQ图片20211203195916.jpg',
        price: 0,
        subjectParentId:'', // 一级分类ID
        subjectId:'', //二级分类ID
        id:''
      },
      teacherList:[],
      subjectOneList:[], //一级分类列表
      subjectTwoList:[],  //二级分类列表
      BASE_API:'http://localhost:8002'
    }
  },
  created() {
    // 初始化教师列表
    this.getTeacherList()
    // 初始化一级分类列表
    this.getOneSubjectList()
    
    this.init()
  },
  
  methods: {
    // 点击下一步触发此方法，根据地址栏的Id值判断是修改还是添加
    next() {
      if(this.$route.params && this.$route.params.id){

        course.updateCourseInfo(this.courseInfo).then(response =>{

          this.$message({
                      type: 'success',
                      message: "修改成功"
                        })
          this.$router.push({ path: '/course/chapter/'+response.data.courseId })
        })
      }else{
        course.addCourseInfo(this.courseInfo).then(response =>{

          this.$message({
                      type: 'success',
                      message: "保存成功"
                        })
          this.$router.push({ path: '/course/chapter/'+response.data.courseId })
        })
      }
    },
    // 如果参数上有ID值，就会初始化courseInfo
    init(){
      if(this.$route.params && this.$route.params.id){
        console.log(this.$route.params.id)
        this.getCourseInfo(this.$route.params.id)
      }
    },
    // 根据courseID得到courseInfo
    getCourseInfo(courseId){

      course.getCourseInfo(courseId).then(response =>{

        this.courseInfo = response.data.courseInfo

        let subjectOneList = this.subjectOneList

        // 根据courseInfo.subjectParentId值 找到哪个一级分类，再把一级分类下的children赋值给二级分类
        for(let i=0;i < subjectOneList.length;i++){

          if(this.courseInfo.subjectParentId == subjectOneList[i].id){

            this.subjectTwoList = subjectOneList[i].children

            return 
            
          }
        }
      })
    }
    ,
    // 上传之前的校验
    beforeAvatarUpload(file){

      const isJPG = file.type === 'image/jpeg'

        const isLt2M = file.size / 1024 / 1024 < 2

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
    },
    // 上传成功之后得后调函数
    handleAvatarSuccess(res,file){
      
        console.log(URL.createObjectURL(file.raw))// base64编码

        this.courseInfo.cover = res.data.url
    },
    // 刷新二级分类列表，vue封装好自动传入一级分类ID
    changeSubjectTwoList(value){

      let subjectOneList =  this.subjectOneList

      for(var i = 0;i <subjectOneList.length;i++){

        if(value === subjectOneList[i].id){

          this.subjectTwoList = subjectOneList[i].children

          this.courseInfo.subjectId = ''
        }
      }
    },
    // 获取一级分类列表
    getOneSubjectList(){

      subject.getSubjectTree()

      .then(response =>{

        this.subjectOneList = response.data.list
      })
    },
    // 获取教师列表
    getTeacherList(){

      course.getTeacherList().then(response=>{

        this.teacherList = response.data.allTeacher

      })
    }
  }
}
</script>
<style scoped>
.tinymce-container {
  line-height: 29px;
}
</style>